package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

import java.util.ArrayList;
import java.util.Stack;

public class LeetCodeTest0653 {

    public static void main(String[] args) {

    }

    private ArrayList<Integer> result = new ArrayList<>();

    public boolean findTarget(TreeNode root, int k) {
        inorderTraversal(root);
        if (result.size() < 2) {
            return false;
        }
        boolean find = false;
        for (int i = 0; i < result.size(); i++) {
            if (result.contains(k - result.get(i)) && result.indexOf(k - result.get(i)) != i) {
                find = true;
                break;
            }
        }
        return find;
    }

    public void inorderTraversal(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> treeNodeStack = new Stack<>();
        while (root != null || !treeNodeStack.isEmpty()) {
            if (root != null) {
                treeNodeStack.push(root);
                root = root.left;
            } else {
                root = treeNodeStack.pop();
                result.add(root.val);
                root = root.right;
            }
        }
    }

}
